#! /usr/bin/python3
# -*- coding: UTF-8 -*-

if __name__ == "__main__":
	raise Exception("不支持从这里调用")

__version__ = "1.0"
__all__ = []

from com.mousechsh.business.lib.MouseChshAuthorizationAnnotition import MouseChshRemoteAuthorizationAnnotition
from com.mousechsh.business.lib.model.MouseChshKeyValueGroupModel import MouseChshKeyValueGroupModel
from com.mousechsh.business.lib.model.MouseChshKeyValueModel import MouseChshKeyValueModel
from com.mousechsh.common.data.MouseChshDao import MouseChshDao
from com.mousechsh.common.data.json.MouseChshJson import mousechsh_json_to_json
from com.mousechsh.common.io.db.MouseChshPostgresql import MouseChshPostgresql
from com.mousechsh.common.middle.http.MouseChshHttpHeader import mousechsh_http_header_accept_language_annotation
from com.mousechsh.common.middle.rest_api.MouseChshRestApi import mousechsh_rest_api_annotation
from com.mousechsh.common.middle.router.MouseChshCorsAnnotition import mousechsh_cors_annotation
from com.mousechsh.common.middle.router.MouseChshHttpRouter import mousechsh_http_router_annotation


@mousechsh_http_router_annotation(method=['POST', 'OPTIONS'], path='/dict/get')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@MouseChshRemoteAuthorizationAnnotition()
@mousechsh_rest_api_annotation()
def _getDict(params):
	lang = params.get('lang', 'zh-cn')
	db = MouseChshPostgresql()
	db.connect()
	data = db.get(
		'''
	   SELECT
		   A."C_Key",
		   COALESCE( A."X_Display" ->> %(lang)s, A."X_Display" ->> ( A."X_Display" ->> '' ) ) AS "C_Display",
		   A."X_Display",
		   A."L_KeyValueGroup_Id",
		   COALESCE( B."X_Display" ->> %(lang)s, B."X_Display" ->> ( B."X_Display" ->> '' ) ) AS "L_KeyValueGroup_Display"
	   FROM "A_Const"."W_KeyValue" A
	   INNER JOIN "A_Const"."W_KeyValueGroup" B
		   ON A."L_KeyValueGroup_Id" = B."C_Id"
	   ORDER BY A."L_KeyValueGroup_Id" ASC, A."C_Order" ASC
	   LIMIT 100
		   ''', {
			'lang': lang
		}
	)
	db.close()
	dao = MouseChshDao()
	dao.set_model(MouseChshKeyValueModel)
	dao.set_extra_model(MouseChshKeyValueGroupModel)
	dicts = dao.parse(data)
	return dicts


@mousechsh_http_router_annotation(method=['POST', 'OPTIONS'], path='/dict/update')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@MouseChshRemoteAuthorizationAnnotition()
@mousechsh_rest_api_annotation()
def _updateDict(params):
	lang = params.get('lang', 'zh-cn')
	kv = MouseChshKeyValueModel()
	kv.set_data(params)
	db = MouseChshPostgresql()
	db.connect()
	db.execute(
		'''
	   UPDATE "A_Const"."W_KeyValue" SET
		   "X_Display" = "X_Display" || %(display)s
	   WHERE "C_Key"   = %(id)s
	   AND   "L_KeyValueGroup_Id" = %(group)s
		   ''', {
			'display': mousechsh_json_to_json(
				{
					lang: kv.get('Display').get(lang, None) if isinstance(kv.get('Display'), dict) else kv.get(
						'Display'
					),
					'': lang
				}
			),
			'id': kv.get('Key'),
			'group': kv.get('KeyValueGroup')
		}
	)
	db.commit()
	db.close()
	return True


@mousechsh_http_router_annotation(method=['POST', 'OPTIONS'], path='/dict/group/update')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@MouseChshRemoteAuthorizationAnnotition()
@mousechsh_rest_api_annotation()
def _updateDictGroup(params):
	lang = params.get('lang', 'zh-cn')
	kv = MouseChshKeyValueGroupModel()
	kv.set_data(params)
	db = MouseChshPostgresql()
	db.connect()
	db.execute(
		'''
	   UPDATE "A_Const"."W_KeyValueGroup" SET
		   "X_Display" = "X_Display" || %(display)s
	   WHERE "C_Id"    = %(id)s
		   ''', {
			'display': mousechsh_json_to_json(
				{
					lang: kv.get('Display').get(lang, None) if isinstance(kv.get('Display'), dict) else kv.get(
						'Display'
					),
					'': lang
				}
			),
			'id': kv.get('Id')
		}
	)
	db.commit()
	db.close()
	return True
